﻿using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;
using System.Windows.Forms;

namespace SdmxMl.SanteSuisse
{
    public class SilcCatiHtml
    {
        private MetadataSetType metadataSet;
        private MetadataStructureDefinitionType msd = null;
        private string template;
        private SmManager sm;


        public SilcCatiHtml(MetadataSetType metaSet, SmManager smMgr)
        {
            sm = smMgr;
            metadataSet = metaSet;

            ArtefactRefTyped art = new ArtefactRefTyped( 
                SdmxArtefactType.MetadataStructureDefinitions, metadataSet.MetadataStructureRef.FullIdent);

            msd = sm.GetArtefact(art) as MetadataStructureDefinitionType;
      //      msd = ReferenceManager.LocateMetadataStructureDefinition(art);        
        }


        public static string NewLineToBreak(string input)
        {
            Regex regEx = new Regex(@"[\n|\r]+");
            return regEx.Replace(input, "<br />");
        }

        public string GenerateRowHtml(SmManager smCati, AttributeValueSetType at, string lg)
        {
            sm = smCati;
            CatiSilcQuestion question = new CatiSilcQuestion(new RefVarHelper(sm), at, lg);
            string silcDir = string.Empty;
            if (string.IsNullOrEmpty(template))
            {
                string appDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
                silcDir = appDir + "\\HTM_TEMPLATE";

                using (StreamReader sr = new StreamReader(appDir + "\\HTM_TEMPLATE\\CATI_Template.htm", Encoding.Default))
                {
                    template = sr.ReadToEnd();
                    sr.Close();
                }
            }

            string html = template.Replace("SILC_DIR", silcDir);

            html = html.Replace("SILC01", question.VarName);
            html = html.Replace("SILC02", question.VarLabel);
            html = html.Replace("SILC03", question.WOrder);

            html = html.Replace("SILC04", question.VarTooltipForFormula(sm, question.Filter));
            html = html.Replace("SILC05", question.VarTooltipForFormula(sm, question.Plausi));

            html = html.Replace("SILC06", NewLineToBreak (question.IntroQuest));

            html = html.Replace("SILC07", NewLineToBreak (question.Question));

            html = html.Replace("SILC08", NewLineToBreak (question.InfoInterwiewer));

            html = html.Replace("SILC09", NewLineToBreak (question.SubQuest));

            // Quality
            html = html.Replace("SILC11", NewLineToBreak(question.QualityCodes));

            // Codes
            html = html.Replace("SILC10", NewLineToBreak(question.VarCodes));
            //QuestionType
            html = html.Replace("SILC12", metadataSet.ReportRef == "R_SILC_TECH" ? "Technical<br>Question" : "Original<br>Question");

            return html;
        }

        private bool FillFromUrnCodelist(AttributeValueSetType at, string conceptId, string lg, StringBuilder sb)
        {
            bool filled = false;
            ReportedAttributeType repAtt = 
                at.ReportedAttributeList.GetReportedAttributeOfConcept(conceptId);
            string urn = repAtt.Value.ToString();
            if (urn.Length > 0)
            {
                filled = true;
                UrnParser upCode = new UrnParser(urn);

                ArtefactRefTyped art = new ArtefactRefTyped(
                    upCode.ArType, upCode.ArtefactId, upCode.AgencyId, upCode.Version);

                CodeListType clt = null;
                try
                {
                    clt = sm.GetArtefact(SdmxArtefactType.CodeLists, art, true) as CodeListType;
                }
                catch 
                {
                    sb.Append("Unable to retrieve codelist: " + art.FullIdent);
                }

                if (clt != null)
                {
                    foreach (CodeType c in clt.CodeList)
                    {
                        sb.Append(c.Id).Append(" - ").Append(c.Name.GetForLanguage(lg));
                        sb.Append("<br />");
                    }
                }
            }
            return filled;
        }
    }
}
